package gps_Convertor;

import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.common.io.Files;

public class GPSConvertor {

	
	
	
	public String convertToGps(HashMap <Integer,Node> nodeMap)
	{
		StringBuilder gpsData2Write = new StringBuilder(); 
		
		for(Map.Entry<Integer, Node> entry : nodeMap.entrySet())
		{
			int sId = entry.getKey(); 
			HashMap<Integer,List<Integer>> connMap = entry.getValue().getConnectionMap(); 
			
			gpsData2Write.append(sId).append(" "); 
			
			for(Map.Entry<Integer, List<Integer>> cMap : connMap.entrySet())
			{
				int pId = cMap.getKey();
				List<Integer> oList = cMap.getValue(); 
				
				for(int oId : oList)
				{
					gpsData2Write.append(oId).append(" ").append(pId).append(" "); 
				}	
			}
			
			gpsData2Write.append("\n"); 
		}
		
		return gpsData2Write.toString(); 
	}
	
	
	public static void writeGpsToFile(File file, String stringToWrite)
	{
		
		Charset charset = Charset.forName("US-ASCII");
		try (BufferedWriter writer = Files.newWriter(file, charset)) {
		    
			writer.write(stringToWrite, 0, stringToWrite.length());
		} catch (IOException ex) {
		    System.err.format("IOException: %s%n", ex);
		}
	}
	
}
